package com.games.portal.rest.resource;

import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import com.games.portal.mng.XmlParserManager;
import com.games.portal.rest.model.Game;
import com.games.portal.rest.model.Games;
 
 
@Path("/gameList")
public class GameResource {
 
         
     
    // The @Context annotation allows us to have certain contextual objects
    // injected into this class.
    // UriInfo object allows us to get URI information (no kidding).
    @Context
    UriInfo uriInfo;
 
    // Another "injected" object. This allows us to use the information that's
    // part of any incoming request.
    // We could, for example, get header information, or the requestor's address.
    @Context
    Request request;
    
    @Context 
    ServletContext servletContext;

    // Basic "is the service running" test
    @GET
    @Path("time")
    @Produces(MediaType.TEXT_PLAIN)
    public String lastUpdatedTimeStamp() {
        return ((Long) servletContext.getAttribute("lastUpdated")).toString();
    }
 
    @GET
    @Path("load")
    @Produces(MediaType.TEXT_PLAIN)
    public String load() {
		Games games = XmlParserManager.unmarshallGamesXml();
		servletContext.setAttribute("gameList", games);
		servletContext.setAttribute("lastUpdated", Calendar.getInstance().getTime().getTime());
    	return games.toString();
    }
    
    @GET
    @Path("get")
    @Produces(MediaType.APPLICATION_JSON)
    public GenericEntity<List<Game>> get() {
    	Games games = (Games)servletContext.getAttribute("gameList");
    	return new GenericEntity<List<Game>>(games.getGames()) {};
    	
    }
        
         
}